﻿using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Microsoft.Extensions.Logging;

namespace K9Nano.LayIM.Events
{
    internal class OfflineNotificationHandler : INotificationHandler<OfflineNotification>
    {
        private readonly IChatManager _chatManager;
        private readonly ILogger _logger;

        public OfflineNotificationHandler(IChatManager chatManager, ILogger<OfflineNotificationHandler> logger)
        {
            _chatManager = chatManager;
            _logger = logger;
        }

        public async Task Handle(OfflineNotification notification, CancellationToken cancellationToken)
        {
            try
            {
                await _chatManager.OfflineAsync(notification, cancellationToken);
            }
            catch (System.Exception ex)
            {
                _logger.LogError(ex, "Failed to handle OfflineNotification for user: " + notification.UserId);
            }
        }
    }
}